﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Crm.Utilities.SolutionsDeploymentTool
{
    internal static class ConfigurationHelper
    {
        #region Constants
        
        private const string CRM_SOURCE_SERVER = "CrmSourceServer";
        private const string CRM_SOURCE_ORGANIZATION = "CrmSourceOrganizationName";

        private const string CRM_SERVICE_TIMEOUT = "ServiceTimeout";

        private const string CRM_TARGET_SERVER = "CrmTargetServer";
        private const string CRM_TARGET_ORGANIZATION = "CrmTargetOrganizationName";

        private const string CRM_DOMAIN = "CrmDomain";
        private const string CRM_USERNAME = "CrmUsername";
        private const string CRM_PASSWORD = "CrmPassword";

        private const string CRM_SOLUTIONS_EXPORT_FOLDER = "ExportFolder";
        private const string CRM_SOLUTION_NAMES = "SolutionNames";
        
        #endregion

        public static string CrmSourceServer
        {
            get
            {
                string keyValue = ConfigurationManager.AppSettings[CRM_SOURCE_SERVER];
                if (string.IsNullOrEmpty(keyValue))
                {
                    throw new ApplicationException("App Setting Key '" + CRM_SOURCE_SERVER + "' should not be null or empty.");
                }
                return keyValue;
            }
            set
            {
                ConfigurationManager.AppSettings[CRM_SOURCE_SERVER] = value;
            }
        }

        public static string CrmSourceOrganization
        {
            get
            {
                string keyValue = ConfigurationManager.AppSettings[CRM_SOURCE_ORGANIZATION];
                if (string.IsNullOrEmpty(keyValue))
                {
                    throw new ApplicationException("App Setting Key '" + CRM_SOURCE_ORGANIZATION + "' should not be null or empty.");
                }
                return keyValue;
            }
            set
            {
                ConfigurationManager.AppSettings[CRM_SOURCE_ORGANIZATION] = value;
            }
        }

        public static string CrmTargetServer
        {
            get
            {
                string keyValue = ConfigurationManager.AppSettings[CRM_TARGET_SERVER];
                if (string.IsNullOrEmpty(keyValue))
                {
                    throw new ApplicationException("App Setting Key '" + CRM_TARGET_SERVER + "' should not be null or empty.");
                }
                return keyValue;
            }
        }

        public static string CrmTargetOrganization
        {
            get
            {
                string keyValue = ConfigurationManager.AppSettings[CRM_TARGET_ORGANIZATION];
                if (string.IsNullOrEmpty(keyValue))
                {
                    throw new ApplicationException("App Setting Key '" + CRM_TARGET_ORGANIZATION + "' should not be null or empty.");
                }
                return keyValue;
            }
        }

        public static string CrmDomain
        {
            get
            {
                return ConfigurationManager.AppSettings[CRM_DOMAIN];
            }
        }

        public static string CrmUsername
        {
            get
            {
                return ConfigurationManager.AppSettings[CRM_USERNAME];
            }
        }

        public static string CrmPassword
        {
            get
            {
                return ConfigurationManager.AppSettings[CRM_PASSWORD];
            }
        }

        public static string CrmSolutionsDirectory
        {
            get
            {
                string keyValue = ConfigurationManager.AppSettings[CRM_SOLUTIONS_EXPORT_FOLDER];
                if (string.IsNullOrEmpty(keyValue))
                {
                    throw new ApplicationException("App Setting Key '" + CRM_SOLUTIONS_EXPORT_FOLDER + "' should not be null or empty.");
                }

                if (!keyValue.EndsWith(@"\"))
                {
                    keyValue += @"\";
                }

                return keyValue;
            }
        }

        public static string[] CrmSolutions
        {
            get
            {
                string keyValue = ConfigurationManager.AppSettings[CRM_SOLUTION_NAMES];
                if (string.IsNullOrEmpty(keyValue))
                {
                    throw new ApplicationException("App Setting Key '" + CRM_SOLUTION_NAMES + "' should not be null or empty.");
                }
                return keyValue.Split(';').Where(m => (m != "") && (m != null)).ToArray();
            }
        }

        public static TimeSpan ServiceTimout
        {
            get
            {
                string keyValue = ConfigurationManager.AppSettings[CRM_SERVICE_TIMEOUT];
                if (string.IsNullOrEmpty(keyValue))
                {
                    return TimeSpan.FromMinutes(2);
                }
                else
                {
                    return TimeSpan.FromMinutes(int.Parse(keyValue));
                }
            }
        }
    }
}
